package com.easy.cloud.web.service.upms.biz.controller;

import cn.hutool.core.lang.tree.Tree;
import com.easy.cloud.web.component.core.response.HttpResult;
import com.easy.cloud.web.component.log.annotation.SysLog;
import com.easy.cloud.web.component.log.annotation.SysLog.Action;
import com.easy.cloud.web.component.security.util.SecurityUtils;
import com.easy.cloud.web.service.upms.api.dto.DeptDTO;
import com.easy.cloud.web.service.upms.api.vo.DeptVO;
import com.easy.cloud.web.service.upms.biz.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Department API
 *
 * @author Fast Java
 * @date 2023-08-03 15:00:02
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "dept")
@Api(value = "部门管理", tags = "部门管理")
public class DeptController {

    private final IDeptService deptService;

    /**
     * 新增
     *
     * @param deptDTO 新增数据
     * @return 新增数据
     */
    @PostMapping(value = "save")
    @PreAuthorize("@pms.hasPermission('system:dept:add')")
    @SysLog(value = "部门新增", action = Action.ADD)
    @ApiOperation(value = "部门新增")
    public HttpResult<DeptVO> save(@Validated @RequestBody DeptDTO deptDTO) {
        // 该接口绑定租户为当前登录用户
        deptDTO.setTenantId(SecurityUtils.getAuthenticationUser().getTenant());
        return HttpResult.ok(deptService.save(deptDTO));
    }

    /**
     * 更新
     *
     * @param deptDTO 新增数据
     * @return 更新数据
     */
    @PostMapping(value = "update")
    @PreAuthorize("@pms.hasPermission('system:dept:update')")
    @SysLog(value = "部门更新", action = Action.UPDATE)
    @ApiOperation(value = "部门更新")
    public HttpResult<DeptVO> update(@Validated @RequestBody DeptDTO deptDTO) {
        // 该接口绑定租户为当前登录用户
        deptDTO.setTenantId(SecurityUtils.getAuthenticationUser().getTenant());
        return HttpResult.ok(deptService.update(deptDTO));
    }

    /**
     * 根据ID移除数据
     *
     * @param deptId ID
     * @return 是否删除成功
     */
    @GetMapping(value = "remove/{deptId}")
    @PreAuthorize("@pms.hasPermission('system:dept:delete')")
    @SysLog(value = "部门删除", action = Action.DELETE)
    @ApiOperation(value = "部门删除")
    public HttpResult<Boolean> removeById(
            @PathVariable @NotBlank(message = "当前ID不能为空") String deptId) {
        return HttpResult.ok(deptService.removeById(deptId));
    }

    /**
     * 根据ID获取详情
     *
     * @param deptId ID
     * @return 详情数据
     */
    @GetMapping(value = "detail/{deptId}")
    @PreAuthorize("@pms.hasPermission('system:dept:query')")
    @SysLog(value = "部门详情", action = Action.FIND)
    @ApiOperation(value = "部门详情")
    public HttpResult<DeptVO> detailById(
            @PathVariable @NotBlank(message = "当前ID不能为空") String deptId) {
        return HttpResult.ok(deptService.detailById(deptId));
    }

    /**
     * TODO 所有数据列表，查询参数自定义
     *
     * @return 查询列表
     */
    @GetMapping(value = "tree")
    @PreAuthorize("@pms.hasPermission('system:dept:query')")
    @SysLog(value = "部门树", action = Action.FIND)
    @ApiOperation(value = "部门树")
    public HttpResult<List<Tree<String>>> tree() {
        return HttpResult.ok(deptService.tree());
    }
}